<?php
namespace Wap\Controller;
use Common\Controller\UsersController;

import("ITK.Itklib.calender");
class SignController extends UsersController {

	public function _initialize(){
        parent::_initialize();
        //访问者控制
        if (!$this->visitor->is_login) {
            IS_AJAX && $this->ajaxReturn(0, L('login_please'));
            $this->redirect('user/login');
        }
        $info = $this->visitor->get();
        $this->assign('info', $info);
        
		$this->_sign_mod = D('Sign');
		$this->_user_mod = D('User');
		$this->_SignLog_mod = D('SignLog');
		$this->_ScoreLog_mod = D('ScoreLog');
    }

    public function index(){
    	$data = array(
    		'uid' => $this->visitor->info['id'],
    	);
    	$sign_date=$this->_sign_mod->where($data)->find();
    	if(!$sign_date){
    		$sign_date['sign_count']=0;
    		$sign_date['sign_ucount']=0;
    		$sign_date['score']=0;
    	}
    	$this->assign('sign',$sign_date);
    	$this->_config_seo(array(
    		'title' => '签到中心',
    	));
    	$this->getSignRank();
    	$this->display();
    }
    
    /**
     * 查询签到排行
     */
    private function  getSignRank(){
    	$order = "sign_count desc";
    	$res=$this->_sign_mod->order($order)->limit(10)->select();
    	$this->assign('allrank',$res);
    	$order = "sign_ucount desc";
    	$res=$this->_sign_mod->order($order)->limit(10)->select();
    	$this->assign('urank',$res);
    	$order = "score desc";
    	$res=$this->_sign_mod->order($order)->limit(10)->select();
    	$this->assign('srank',$res);
    }
    
	public function ajax_sign(){	
		if(!IS_AJAX) return;
		$data = array(
                'uid' => $this->visitor->info['id'],
                'username' => $this->visitor->info['username'],
            );
		$_r=array(
			'status'=>0,
			'msg'=>'',
			'score'=>0,
		);
		$point_score = $this->point();
        if (!$sign_date=$this->_sign_mod->where($data)->find()) {
			//如果从未签到过

			/*积分奖励算法*/
			//$this->_user_mod->where($data)->setInc('score',$this->point());
			//$this->_user_mod->where($data)->setField('sign_time',strtotime(date('Ymd')));

			$score_data = array('score'=>array('exp','score+'.$point_score), 'sign_time'=>strtotime(date('Ymd')));
            $this->_user_mod->where(array('id'=>$data['uid']))->setField($score_data); //改变用户积分

			//更新为已经签到
            $data['score']=$point_score;
			$this->_sign_mod->create($data);
            $this->_sign_mod->add();
			//添加一条签到记录
			$data['score'] = $point_score;
			$this->_SignLog_mod->create($data);
			$this->_SignLog_mod->add();
			//添加一条积分记录
			$ScoreLog_data['uid']		=	$this->visitor->info['id'];
			$ScoreLog_data['uname']	=	$this->visitor->info['username'];
			$ScoreLog_data['action']	=	'sign';
			$ScoreLog_data['score']	=	$point_score;
			$this->_ScoreLog_mod->create($ScoreLog_data);
			$this->_ScoreLog_mod->add();
			
			$_r['msg']="恭喜您签到成功，<br/>奖励{$point_score}积分<br/>";

		}else{
			//如果签到过
			//$sign_date=$this->_sign_mod->where($data)->find();
			$last_date=strtotime(date('Y-m-d'));
			if($sign_date['last_date']==$last_date){
				//今天已经签到

				$_r['status']=-1;
				$_r['msg']='今天您的签到机会已用完，<br/>明天再接再励哦！';

				$this->ajaxReturn(1, L('sign_system'), $_r);

			}else if($sign_date['last_date']+86400==$last_date){
				//今天未签到昨天签到过
				if($sign_date['sign_ucount'] +1 >= C('itk_score_rule.sign_day')){
					$point_score+=C('itk_score_rule.sign_add');
					$this->_sign_mod->where($data)->setField(array('sign_ucount'=>1));		//设置为0
				}else{
					$this->_sign_mod->where($data)->setInc('sign_ucount');		//连续签到+1
				}
				$score_data = array('score'=>array('exp','score+'.$point_score), 'sign_time'=>strtotime(date('Ymd')));
				$this->_user_mod->where(array('id'=>$data['uid']))->setField($score_data); //改变用户积分

				//更新为已经签到
				$this->_sign_mod->where($data)->setInc('sign_count');
				
				$this->_sign_mod->where($data)->setInc('score',$point_score);
				$this->_sign_mod->where($data)->setField('last_date',strtotime(date('Ymd')));
				
				//添加一条签到记录
				$data['score'] = $point_score;
				$this->_SignLog_mod->create($data);
				$this->_SignLog_mod->add();
				//添加一条积分记录
				$ScoreLog_data['uid']		=	$this->visitor->info['id'];
				$ScoreLog_data['uname']	=	$this->visitor->info['username'];
				$ScoreLog_data['action']	=	'sign';
				$ScoreLog_data['score']	=	$point_score;
				$this->_ScoreLog_mod->create($ScoreLog_data);
				$this->_ScoreLog_mod->add();
				$_r['msg']="已连续签到成功，<br/>奖励{$point_score}积分,<br/>明天再接再励！";
				
			}else{
				//无连续签到  从头算起

				/*积分奖励算法*/
				//$this->_user_mod->where($data)->setInc('score',$point_score);
				//$this->_user_mod->where($data)->setField('sign_time',strtotime(date('Ymd')));
				$score_data = array('score'=>array('exp','score+'.$point_score), 'sign_time'=>strtotime(date('Ymd')));
				$this->_user_mod->where(array('id'=>$data['uid']))->setField($score_data); //改变用户积分

				//更新为已经签到
				$sign_data=$data;
				$sign_data['sign_count'] = $sign_date['sign_count']+1;
				$sign_data['score'] = $sign_date['score']+$point_score;
				$sign_data['sign_ucount'] = '1';
				$sign_data['last_date'] = strtotime(date('Ymd'));
				$this->_sign_mod->where($data)->save($sign_data);
				//添加一条签到记录
				$data['score'] = $point_score;
				$this->_SignLog_mod->create($data);
				$this->_SignLog_mod->add();
				//添加一条积分记录
				$ScoreLog_data['uid']		=	$this->visitor->info['id'];
				$ScoreLog_data['uname']	=	$this->visitor->info['username'];
				$ScoreLog_data['action']	=	'sign';
				$ScoreLog_data['score']	=	$point_score;
				$this->_ScoreLog_mod->create($ScoreLog_data);
				$this->_ScoreLog_mod->add();
				$_r['msg']="恭喜您签到成功，<br/>奖励{$point_score}积分<br/>";
			}
		}
// 		$cl = new \calender();
// 		$data['table']=$cl->sign_calender();
// 		$data['point']=$point_score;
// 		if($sign_info = $this->_sign_mod->field('id,username,last_date,sign_count')->where(array('uid'=>$this->visitor->info['id']))->find()){
// 			if($sign_info['sign_count']>= C('itk_score_rule.sign_day')){
// 				$data['tmr_point']=$point_score;
// 			}else{
// 				$data['tmr_point']=$point_score + C('itk_score_rule.sign_add');
// 			}
// 		}
//		$_r['msg']='恭喜您已连续签到成功，<br/>奖励"+data.add_sore+"积分<br/>';
		
		$this->ajaxReturn(1, L('sign_system'), $_r);

	}

	/**
	 * 计算用户签到积分
	 * @uthor Caption
	 */
	public function point(){
		$jinbi = C('itk_score_rule.sign');
		$sign_mod = M('sign');
		$sign_info = $sign_mod->field('id,username,last_date,sign_count')->where(array('uid'=>$this->visitor->info['id']))->find();
		$last_date=strtotime(date('Y-m-d'));
		if($sign_info){
			if($sign_info['last_date']==$last_date){	
				//今天已经签到
// 				if($sign_info['sign_count']>=1){
// 					$jinbi = C('itk_score_rule.sign') + ($sign_info['sign_count'])*C('itk_score_rule.sign_add'); 
// 					if($sign_info['sign_count']>= C('itk_score_rule.sign_day')){
// 						$jinbi = C('itk_score_rule.sign') +  (C('itk_score_rule.sign_day')-1)*C('itk_score_rule.sign_add'); 
// 					}
// 				}else{
// 					$jinbi = C('itk_score_rule.sign');
// 				}

				$jinbi=0;		
			}else if($sign_info['last_date']+86400==$last_date){
				// 连续签到
				if($sign_info['sign_ucount']>= C('itk_score_rule.sign_day') ){
					$jinbi = C('itk_score_rule.sign') + C('itk_score_rule.sign_add'); 
				}else{	//非连续签到
					$jinbi = C('itk_score_rule.sign');
				}		
			}else{
				$jinbi = C('itk_score_rule.sign');
			}
		}
		return $jinbi;
	}

	public function signstatus(){		
		$sign_mod=M('sign');
		$sign_info = $sign_mod->field('id,username,last_date,sign_count')->where(array('uid'=>$this->visitor->info['id']))->find();
		$last_date=strtotime(date('Y-m-d'));
		if($sign_info['last_date']==$last_date){
			if($sign_info['sign_count']>1){
				$jinbi = C('itk_score_rule.sign') + ($sign_info['sign_count']-1)*C('itk_score_rule.sign_add'); 
				if($sign_info['sign_count']> C('itk_score_rule.sign_day')){
					$jinbi = C('itk_score_rule.sign') +  (C('itk_score_rule.sign_day')-1)*C('itk_score_rule.sign_add'); 
				}
			}else{
				$jinbi = C('itk_score_rule.sign');
			}
			$user_mod=M('user');
			$user_info = $user_mod->field('id,username,score')->where(array('id'=>$this->visitor->info['id']))->find();


			$result['today'] = '已签到 + '.$jinbi;
			$result['score'] = $user_info['score'];
			$this->ajaxReturn(1, 'ok', $result);		
		}else{
			$this->ajaxReturn(0, 'sign_out');	
		
		}
	}
	/**
	 * 查询签到记录
	 */
	public function record(){
		$data = array(
    		'uid' => $this->visitor->info['id'],
    	);
		$sign_date=$this->_SignLog_mod->where($data)->order("id desc")->select();
		$this->assign('sign_log',$sign_date);
		$this->display();
	}
}